package com.fanhang.samples.spring.jdbc.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.stereotype.Repository;

import com.fanhang.samples.spring.jdbc.common.BaseJdbcDaoSupport;
import com.fanhang.samples.spring.jdbc.dao.RoleDao;
import com.fanhang.samples.spring.jdbc.po.Role;

@Repository
public class RoleDaoImpl extends BaseJdbcDaoSupport<Role> implements RoleDao {

	@Override
	public Role get(Long id) {
		String sql = "SELECT * FROM stat_role WHERE id = ?";
		return super.get(sql, new Long[]{id});
	}
	

	@Override
	public List<Role> getList(Role model) {
		StringBuilder sql = new StringBuilder("SELECT * FROM stat_role WHERE 1=1 ");
		if (model.getParentId() != null) {
			sql.append(" and parent_id = :parentId ");
		}
		if (model.getAreaId() != null) {
			sql.append(" and area_id = :areaId ");
		}
		if (!StringUtils.isEmpty(model.getName())) {
			sql.append(" and name like '%:name%' ");
		}
		return super.getList(sql.toString(), model);
	}
	

	@Override
	public int save(Role model) {
		String sql = "insert into stat_user(id,login_name,password,user_name,role_id,status,creator,create_time) values (:id, :loginName, :password, :userName, :roleId, :status, :creator, :createTime)";
		return super.update(sql, model);
	}

	@Override
	public int delete(Role model) {
		String sql = "update employee set name=:name,birthday=:birthday,status=:status WHERE id = :id";
		return super.update(sql, new BeanPropertySqlParameterSource(model));
	}

	@Override
	public int update(Role model) {
		String sql = "update employee set name=:name,birthday=:birthday,status=:status WHERE id = :id";
		return super.update(sql, new BeanPropertySqlParameterSource(model));
	}
}
